package models

import (
	"2302a_news/appconfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Init() {
	mysqlConf := appconfig.Appcfg.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysqlConf.User, mysqlConf.Password, mysqlConf.Host, mysqlConf.Port, mysqlConf.DataBase)
	var err error
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	} else {
		fmt.Println("连接成功")
	}
	err = DB.AutoMigrate(&User{}, &News{})
	if err != nil {
		panic(err)
	} else {
		fmt.Println("迁移成功")
	}
}
